//
//  MarketMainViewController.m
//  Nidalee
//
//  Created by neobug on 2020/12/1.
//  Copyright © 2020 李康. All rights reserved.
//

#import "MarketMainViewController.h"

#import "MarketListCell.h"
 
#import <Masonry/Masonry.h>
#import "NetWorkManager.h"
#import <MJRefresh/MJRefresh.h>
#import "MarketLoadService.h"

#define kLince @"A360FAAC-494B-AC6F-3913-67ADB6C9001C"

@interface MarketMainViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *view_content;

@property (weak, nonatomic) IBOutlet UIView *sz_back;
@property (weak, nonatomic) IBOutlet UIView *sh_back;
@property (weak, nonatomic) IBOutlet UIView *cy_back;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

/* 0-上证  1-深证  2-创业 */
@property (nonatomic, assign) NSInteger type;
/* 上证指数*/
@property (nonatomic, strong) NSMutableArray *sz_data;
/* 深证指数*/
@property (nonatomic, strong) NSMutableArray *sh_data;
/* 创业指板*/
@property (nonatomic, strong) NSMutableArray *cy_data;

@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation MarketMainViewController


#pragma mark - Public Method

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initAllPropertys];
    [self setupUI];
    [self addTap];
    [self queryData];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.hidesBottomBarWhenPushed = NO;
}
- (void)dealloc {
    NSLog(@"%s",__func__);
}
#pragma mark - Data Query
- (void)queryData {
    NSArray *arr = [MarketLoadService MLSLoadData];
    NSLog(@"%@",arr);
    
    NetWorkManager *manager = [[NetWorkManager alloc] init];
    [manager Post_url:[NSString stringWithFormat:@"http://ig507.com/data/base/gplist?licence=%@",kLince] params:nil success:^(id  _Nonnull response) {
        NSLog(@"%@",response);
    } fauilre:^(NSString * _Nonnull error) {
        NSLog(@"%@",error);

    }];
    [self.tableView reloadData];
}
#pragma mark - Notification

#pragma mark - UI
- (void)initAllPropertys {
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:218/255.0 green:165/255.0 blue:32/255.0 alpha:1.0];
    self.title = @"行情";
    self.type = 0;
}

- (void)setupUI {
    [self changeStatus:self.sz_back select:YES];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"MarketListCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MarketListCell"];
    self.tableView.tableFooterView = [UIView new];
    __weak __typeof__(self) weakSelf = self;
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        [weakSelf queryData];
//    }];
//    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        [weakSelf queryData];
//    }];
}
#pragma mark - Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MarketListCell *cell = (MarketListCell *)[tableView dequeueReusableCellWithIdentifier:@"MarketListCell" forIndexPath:indexPath];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - Private Method
- (void)changeStatus:(UIView *)backView select:(BOOL)select {
    if (select) {
        backView.layer.shadowColor = [UIColor grayColor].CGColor;
        backView.layer.shadowOffset = CGSizeMake(2, 2);
        backView.layer.shadowOpacity = 0.5;
        backView.layer.cornerRadius = 3;
        backView.backgroundColor = [UIColor colorWithRed:218/255.0 green:165/255.0 blue:32/255.0 alpha:1.0];
    }else {
        backView.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.0];
    }
}

- (void)addTap {
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changSZ)];
    [self.sz_back addGestureRecognizer:tap1];
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changSH)];
    [self.sh_back addGestureRecognizer:tap2];
    
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changCY)];
    [self.cy_back addGestureRecognizer:tap3];
}
#pragma mark - Overwrite Method

#pragma mark - Event Response
- (void)changSZ {
    [self changeStatus:self.sz_back select:YES];
    [self changeStatus:self.sh_back select:NO];
    [self changeStatus:self.cy_back select:NO];
}

- (void)changSH {
    [self changeStatus:self.sz_back select:NO];
    [self changeStatus:self.sh_back select:YES];
    [self changeStatus:self.cy_back select:NO];

}

- (void)changCY {
    [self changeStatus:self.sz_back select:NO];
    [self changeStatus:self.sh_back select:NO];
    [self changeStatus:self.cy_back select:YES];

}
#pragma mark - Setter
- (void)setType:(NSInteger)type {
    switch (type) {
        case 0:
        {
            self.dataSource = [self.sz_data mutableCopy];
        }
            break;
        case 1:
        {
            self.dataSource = [self.sh_data mutableCopy];
        }
            break;
        case 2:
        {
            self.dataSource = [self.cy_data mutableCopy];
        }
            break;
        default:
            break;
    }
}
#pragma mark - Getter
- (NSMutableArray *)sz_data {
    if (!_sz_data) {
        _sz_data = [NSMutableArray array];
    }
    return _sz_data;
}

- (NSMutableArray *)sh_data {
    if (!_sh_data) {
        _sh_data = [NSMutableArray array];
    }
    return _sh_data;
}

- (NSMutableArray *)cy_data {
    if (!_cy_data) {
        _cy_data = [NSMutableArray array];
    }
    return _cy_data;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
@end
